package smashedapples.core 
{	

	import flash.events.EventDispatcher;
	
	import smashedapples.events.ValueObjectEvent;
	import smashedapples.utils.ValueObjectUtils;
	
	[Event(name="set", type="smashedapples.events.ValueObjectEvent")]
	
	[Bindable]
	dynamic public class ValueObject extends EventDispatcher
	{	
		
		
		public function ValueObject( data:* = null ):void
		{
			this.setVO( data );
		}

		public function setVO( data:*=null ):void
        {    
			 if(data == null)
                return;
            
            for ( var key:String in data )
            {	
            	var handlerName:String = getSetHandlerFunctionName( key );
			
				if( key != "mx_internal_uid" && this.hasOwnProperty( handlerName ) )
				{
					var method : Function = this[handlerName] as Function;
					method.apply( this, [ data[ key ] ] );
				}
				else if( key != "mx_internal_uid" )
				{
	       			this[ key ] = data[ key ];
				}
            }
            
            this.dispatchEvent( new ValueObjectEvent( "set", this ) );
        }
		
		
		protected function getSetHandlerFunctionName( key:String ):String
		{
           return "set" + key.substr( 0, 1 ).toUpperCase() + key.slice( 1 );
		}
		
	
		/* public function get query():XML
		{
			return this.toXML();
		} */
		
		public function toXML():XML
		{
			var xml:XML = new XML({});
			return xml;
		}
		
		public function voArrayToXMList( source:Array ):XMLList
		{
			return ValueObjectUtils.voArrayToXMList( source );	
		}
		

	}
}